//
//  LTLComposeViewController.m
//  
//
//  Created by Apple_Lzzy45 on 16/11/3.
//  Copyright © 2016年 Apple_Lzzy40. All rights reserved.
//

#import "LTLComposeViewController.h"
#import "LTLTextView.h"
#import "LTLgongJu.h"
#import "LTLKeyboard.h"

@interface LTLComposeViewController ()<UITextViewDelegate,LTLComposeToolbarDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
/*输入*/
@property(nonatomic,strong)LTLTextView *textView;
/*工具条*/
@property(nonatomic,strong)LTLgongJu *toolbar;
/**
 *  键盘
 */
@property(nonatomic ,strong)LTLKeyboard *Keyboard;

@property (nonatomic,assign)BOOL isAnimate;

@end

@implementation LTLComposeViewController

-(LTLKeyboard *)Keyboard
{
    if (!_Keyboard) {
        _Keyboard= [[LTLKeyboard alloc]init];
//        _Keyboard.backgroundColor =[UIColor blueColor];
        _Keyboard.width = self.view.width;
        _Keyboard.highly = 258;
    }
    return _Keyboard;
}

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置导航栏
    [self setupNavi];
    _isAnimate = YES;
    //添加输入按钮
    [self setupTextView];
    //添加工具条
    [self setupToolbar];
//    self.navigationItem.rightBarButtonItem.enabled=NO;
}
///即将显示
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //有文本
    //发送按钮可用
    self.navigationItem.rightBarButtonItem.enabled = self.textView.hasText;
    
    
}

//控件销毁时执行
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    //隐藏键盘
    [self.textView resignFirstResponder];

}

//添加输入控件
-(void)setupTextView
{

    LTLTextView *textView =[[LTLTextView alloc]init];
//       textView.bounds.size = self.view.bounds.size;
    textView.width = self.view.width;
    textView.highly = self.view.highly;
    textView.font = [UIFont systemFontOfSize:18];
    textView.x = 0;
    textView.y = 0;
   // textView.
    textView.placeholder=@"随时随地分享新鲜事....";
    textView.placeholderColor = [UIColor grayColor];
    //设置弹簧效果
    textView.alwaysBounceVertical =YES;
    
    
    textView.backgroundColor =[UIColor whiteColor];
    
    self.textView =textView;
    [self.view addSubview:textView];
    //h获得焦点
    [self.textView  becomeFirstResponder];
    
    self.textView.delegate = self;

    //注册 监听文本事件
//  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:textView];
   //注册 监听键盘frame改变事件
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
//    
}
/**
 *  监听键盘frame改变事件
 *
 */
-(void)KeyboardWillChangeFrame:(NSNotification *)notification
{

//    NSLog(@"监听键盘frame改变事件%@",notification);
    
    NSDictionary *userInfo =notification.userInfo;
    //键盘frame

    CGRect FrameEnd=[userInfo [UIKeyboardFrameEndUserInfoKey]CGRectValue];
  
    //动画持续时间
    double duration =[userInfo [UIKeyboardAnimationDurationUserInfoKey]doubleValue];
    if (_isAnimate) {
        
        //执行动画
        [UIView animateWithDuration:duration animations:^{
            //设置工具条的y =键盘的y  -工具条高度
            self.toolbar.y =FrameEnd.origin.y - self.toolbar .highly;
            _textView.highly = CGRectGetMinY(_toolbar.frame);
        }];
    }
}
////监听文本改变事件
//-(void)textChange
//{
//    self.navigationItem.rightBarButtonItem.enabled =self.textView.hasText;
//}
//添加工具条
-(void)setupToolbar
{
    LTLgongJu *toolbar =[[LTLgongJu alloc]init];
    toolbar.width =self.view.width;
    toolbar.highly =44;
    
    toolbar.y=self.view.highly -toolbar.highly;
    [self.view addSubview:toolbar];
    toolbar.delegate = self;
    self.toolbar=toolbar;
    //指定代理
//    self.toolbar.delegate=self;
    //inputView设置键盘输入法
    //self.textView.inputAccessoryView =toolbar;
    
}
//实现代理
-(void)composeToobar:(LTLgongJu *)toolbar didClickBtn :(LTLComposeToolbarButtonType) type
{
    switch (type) {
        case LTLComposeToolbarButtonTypePicture:
            NSLog(@"相册");
            //打开相册
            [self openAlbum];
            break;
           
        case LTLComposeToolbarButtonTypeMention:
            NSLog(@"@");
            
            
            break;
        case LTLComposeToolbarButtonTypeTrend:
            NSLog(@"#");
            break;
        case LTLComposeToolbarButtonTypeEmontion:
            NSLog(@"表情");
            [self openEmontion];
            break;
        case LTLComposeToolbarButtonTypeCamera:
            NSLog(@"拍照");
            break;
        default:
            break;
    }

}
/**
 *  打开表情
 */
-(void)openEmontion
{
    //判断是键盘还是表情键盘
    if (self.textView.inputView == nil) {           //不是表情键盘
        self.textView.inputView = self.Keyboard;    //辅助键盘
        self.toolbar.keyExpression = YES;           //设置图片
    } else {                                         //不是表情键盘
        self.textView.inputView = nil;               //关了辅助键盘
        self.toolbar.keyExpression = NO;
    }

    _isAnimate = NO;           //不要动画
    
    if (_toolbar.y ==self.view.highly -_toolbar.highly) {     ///工具条在屏幕底下就需要动画
        _isAnimate = YES;
    }
    
    [self.textView resignFirstResponder];   //关闭键盘
    [self.textView becomeFirstResponder];   //打开键盘
    _isAnimate = YES;                       //打开工具条动画开关
    
}

/*打开相册*/
-(void)openAlbum
{
//    //隐藏键盘
//    [self.textView resignFirstResponder];
    //图片选择控制器
    UIImagePickerController *ipc =[[UIImagePickerController alloc]init];
    
    //图片资源类型 :图库
    ipc.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
    
    //指定代理
    ipc.delegate =self;
    _toolbar.y=self.view.highly -_toolbar.highly;
    [self presentViewController:ipc animated:YES completion:^{
        //    //隐藏键盘
//        _toolbar.y=self.view.highly -_toolbar.highly;
    }];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
//    NSLog(@"%@",info);
    [picker dismissViewControllerAnimated:YES completion:nil];
    //info包含了选中图片
    UIImage *image =info[UIImagePickerControllerOriginalImage];
    //把图片添加到textview
    [self.textView.photoView addPhome:image];
    
}

/**
 *  点击取消时调用
 */
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    //    DDActionLog;
    // 关闭图片选择器
    [picker dismissViewControllerAnimated:YES completion:nil];
}
//设置导航栏
-(void)setupNavi
{    //设置导航栏
    self.view.backgroundColor =[UIColor blueColor];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
    self.navigationItem.rightBarButtonItem.enabled =NO;
    //获取用户名称
    NSString * name =[LTLModel read].name;
    NSLog(@"%@",name);
    //
    NSString *prefix = @"发微博";
    UILabel *titleView =[[UILabel alloc]init];
    
    NSString *title =[NSString stringWithFormat:@"%@\n%@",prefix,name];
    titleView.width =200;
    titleView.highly =44;
    titleView.numberOfLines = 0;
    titleView.textAlignment = NSTextAlignmentCenter;
//      titleView.backgroundColor=[UIColor redColor];
    
    //创建带有属性的字符串
    NSMutableAttributedString *attributeTitle =[[NSMutableAttributedString alloc]initWithString:title];
    
    //获取 prefix字串Range
    NSRange prefixRange =[title rangeOfString:prefix];
    // NSLog(@"",prefixRange.location,)
    [attributeTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:prefixRange];
    //添加属性
    //    NSRange range;
    //    range.length = 3;
    //    range.location =0;
    NSRange nameRange =[title rangeOfString:name];
    [attributeTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:nameRange];
    [attributeTitle addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:nameRange];
    titleView.attributedText =attributeTitle;
    
    self.navigationItem.titleView=titleView;
    

}

//取消
-(void)cancel
{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.textView resignFirstResponder];
}
//发送
-(void)send
{
    NSLog(@"send--%@",self.textView.text);
    
    self.navigationItem.rightBarButtonItem.enabled = NO;
    
    UIImage *image = [self.textView.photoView.PhomeArray firstObject];
    
    [SVProgressHUD showWithStatus: @"发送中!"];
    
    [LTLNetworkRequest faSong:self.textView.text iamge:image LTL:^{
        [self dismissViewControllerAnimated:YES completion:nil];
    } error:^{
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }];
    
    
}
//文本视图改变了
- (void)textViewDidChangeSelection:(UITextView *)textView
{
//    //没有文本
//    if (textView.text.length != 0 ) {
        //发送按钮可用
    self.navigationItem.rightBarButtonItem.enabled = textView.hasText;
        
//    } else {//没有文本
//        //发送按钮不可用
//        self.navigationItem.rightBarButtonItem.enabled = NO;
//        
//    }
}

@end
